package ui.maps.googlemap{
	
	import com.google.maps.*;
	import com.google.maps.controls.*;
	import com.google.maps.overlays.*;
	import com.google.maps.services.*;
	
	import common.cache.SystemCache;

	public class GoogleMapService extends Map {
		
		
		private var mapDefaultCenter:LatLng = new LatLng(SystemCache.center.lat,SystemCache.center.lng);//设置地图默认中心;
		private var _mapDefaultZoom:Number;
		
		private var mapTypeControl:MapTypeControl = new MapTypeControl();
		private var overviewMapControl:OverviewMapControl = new OverviewMapControl();
		private var scaleControl:ScaleControl = new ScaleControl();
		private var navigationControl:NavigationControl = new NavigationControl();
		
		public var simpleStyle:Boolean = false;
		public var satelliteMapType:Boolean = false;
		
		public function GoogleMapService() {
			super();
			this.key = SystemCache.googleApiKey;
			this.sensor="true";
			addEventListener(MapEvent.MAP_PREINITIALIZE, onMapPreinitialize);
			addEventListener(MapEvent.MAP_READY, onMapReady);
			this.language="zh-CN";
		}  
		
		public function get mapDefaultZoom():Number
		{
			return _mapDefaultZoom;
		}
		
		public function set mapDefaultZoom(value:Number):void
		{
			_mapDefaultZoom = value;
		}
		
		private function onMapPreinitialize(event:MapEvent):void {
			initMapOptions();
		}
		
		private function onMapReady(event:MapEvent):void{
			initMapDefaultAction();
			addMapControl();
		}
		
		//地图默认参数
		private function initMapOptions():void{
			var myMapOptions:MapOptions = new MapOptions();
			myMapOptions.center = mapDefaultCenter;
			myMapOptions.zoom = mapDefaultZoom;
			myMapOptions.mapType = satelliteMapType?MapType.HYBRID_MAP_TYPE:MapType.NORMAL_MAP_TYPE;
			this.setInitOptions(myMapOptions);
		}
		
		//地图默认功能
		private function initMapDefaultAction():void{
			this.enableScrollWheelZoom(); //鼠标滚轮放大缩小方法,方法调用
			this.enableContinuousZoom();  //平滑放大缩小方法
			this.enableControlByKeyboard();
			this.enableCrosshairs();
			this.enableDragging();
		}
		
		
		private function addMapControl():void{
			if(isLoaded() && simpleStyle == false){
				setSimpleStyle(simpleStyle);
			}
		}
		
		private var hadAddControl:Boolean = false;
		
		public function setSimpleStyle(simpleStyle:Boolean):void{
			try{
				if(simpleStyle == true){
					if(hadAddControl == true){
						//						removeControl(mapTypeControl);
						removeControl(overviewMapControl);
						removeControl(scaleControl);
						removeControl(navigationControl);
						hadAddControl = false;
					}
				}else if(hadAddControl==false){
					//					addControl(mapTypeControl);
					addControl(overviewMapControl);
					addControl(scaleControl);
					addControl(navigationControl);
					hadAddControl = true;
				}
			}catch(e:Error){}
		}
	}
}